package com.dudu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.dudu.bean.ParamBean;
import com.dudu.bean.PoolAbilityInfoBean;
import com.dudu.bean.PoolNumberBean;
import com.dudu.dao.CallStatusDao;
import com.dudu.dao.PoolAbilityInfoDao;
import com.dudu.dao.PoolNumberDao;
import com.dudu.model.PoolAbilityInfo;
import com.dudu.model.PoolNumber;
import com.dudu.service.IPoolNumberService;

import core.CoreDao;
import core.CoreDaoInvoke;
/**
 * 号码池管理 service
 * @author gaoxs
 *
 */
public class PoolNumberServiceImpl implements IPoolNumberService {
	private CoreDao coreDao = CoreDaoInvoke.getInstance();
	private CallStatusDao callstatusDao =new CallStatusDao();
	private PoolNumberDao poolNumberDao =new PoolNumberDao();
	// 计费规则设置
	//private CharginRuleDao charginRuleDao=new CharginRuleDao();
	private PoolAbilityInfoDao poolAbilityInfoDao = new PoolAbilityInfoDao();
			
	/**
     * 查询所有省份的名称
     * @return
     */
	public List<ParamBean> queryAllAreas(){
		return callstatusDao.queryAllAreas(coreDao);
	}
	/**
	 * 查询号码池列表分页
	 * @param pageSql
	 * @param paramSql
	 * @return
	 */
	public List<PoolNumberBean> queryAll(String pageSql,String paramSql){
		List<PoolNumber> list=this.poolNumberDao.queryAllNumber(coreDao, pageSql, paramSql);
		List<PoolNumberBean> result=new ArrayList<PoolNumberBean>();
		if(null ==list)
			return null;
		for(PoolNumber pool :list){
			result.add(conversion(pool));
		}
		return result;
	}
	/**
	 * 查询号码池总数
	 * @param paramSql
	 * @return
	 */
	public int getTotalNumber(String paramSql){
		return poolNumberDao.getTotalNumber(coreDao, paramSql);
	}
	/**
	 * 新增号码池数据
	 * @param bean
	 * @return
	 */
	public int doInsert(PoolNumberBean bean){
		return poolNumberDao.insert(coreDao, conversion(bean));
	}
	/**
	 * 修改号码池数据
	 * @param bean
	 * @return
	 */
	public int doEdit(PoolNumberBean bean){
		return  poolNumberDao.update(coreDao, conversion(bean));
	}
	
	/**
	 * 根据能力类型，查询所有已配置能力的号码list 分页
	 * @param pageSql
	 * @param paramSql
	 * @param accountId 获取账号id
	 * @param abilityType  获取能力类型
	 * @return
	 */
	public List<PoolNumberBean> queryNumbersByAbility(String pageSql,String paramSql,String accountId,String abilityType){
		List<PoolNumberBean> result=new ArrayList<PoolNumberBean>();
		//开始查询账号信息
		List<PoolNumber>  numberPool=this.poolNumberDao.queryNumbersByAbility(coreDao, pageSql, paramSql, accountId, abilityType);
		if(null == numberPool)return null;
		for(PoolNumber pool:numberPool){
			result.add(conversion(pool));
		}
		return result;
	}
	/**
	 * 根据能力类型，查询所有已配置能力的号码总数
	 * @param paramSql
	 * @param accountId
	 * @param abilityType
	 * @return
	 */
	public int getTotalNumberByAbility(String paramSql, String accountId, String abilityType){
		return this.poolNumberDao.getTotalNumberByAbility(coreDao, paramSql, accountId, abilityType);
	}
	/**
	 * 根据能力类型，查询所有未配置能力的号码总数
	 * @param paramSql
	 * @param abilityType
	 * @param app_key
	 * @param cust_account
	 * @return
	 */
	public int getTotalUnConfNum( String paramSql, String abilityType,String app_key,String cust_account){
		return this.poolNumberDao.getTotalUnConfNum(coreDao, paramSql, abilityType, app_key, cust_account);
	}
	/**
	 * 根据能力类型，查询所有未配置能力的号码 list 分页
	 * @param pageSql
	 * @param paramSql
	 * @param abilityType
	 * @param accountId
	 * @return
	 */
	public List<PoolNumberBean> queryUnConfNumByAbility(String pageSql,String paramSql,String abilityType,String app_key,String cust_account){
		List<PoolNumberBean> result=new ArrayList<PoolNumberBean>();
		//开始查询账号信息
		List<PoolNumber>  numberPool=this.poolNumberDao.queryUnConfNumByAbility(coreDao, pageSql, paramSql, abilityType, app_key,cust_account);
		if(null == numberPool)return null;
		for(PoolNumber pool:numberPool){
			result.add(conversion(pool));
		}
		return result;
	}
	/**
	 * 根据号码池中号码的id，获取该号码已配置的号码能力关系
	 * @param poolId
	 * @return
	 */
	public List<PoolAbilityInfoBean> getRelByPoolId(String poolId){
		List<PoolAbilityInfoBean> result=new ArrayList<PoolAbilityInfoBean>();
		List<PoolAbilityInfo> list=poolAbilityInfoDao.getRelByPoolId(coreDao, poolId);
		if(null==list)return null;
		for(PoolAbilityInfo pool:list){
			result.add(conversion(pool));
		}
		return result;
	}
	/**
	 * 根据号码池中号码的id，获取该号码已配置的号码能力关系
	 * @param pool_ability_id
	 * @return
	 */
	public PoolAbilityInfoBean getPoolAbilityNumberById(String pool_ability_id){
		//返回值
		PoolAbilityInfoBean result=null;
		//查询相应信息
		PoolAbilityInfo poolAbilityInfo=poolAbilityInfoDao.queryPoolAbilityNumberById(coreDao, pool_ability_id);
		//判断如果查询到的值为null，则返回null
		if(poolAbilityInfo==null){
			result=null;
		}else{
			//进行实体类转化
			result=conversion(poolAbilityInfo);
		}
		return result;
	}
	/**
	 * 查询默认显号
	 * @param dao
	 * @param ability_type  能力
	 * @param number_source 来源（号码来源：通道提供：external，用户提供：custom，内部号码：inside,公共号码池：general）
	 * @return
	 */
	public PoolAbilityInfoBean queryDefaultPoolAbilityNumber(String ability_type, String number_source){
	    PoolAbilityInfoBean result=null;
	     //查询相应信息
	    PoolAbilityInfo poolAbilityInfo=poolAbilityInfoDao.queryDefaultPoolAbilityNumber(coreDao, ability_type, number_source);
	    if(poolAbilityInfo==null){
		result=null;
	    }else{
		//进行实体类转化
		result=conversion(poolAbilityInfo);
	    }
	return result;
	}
	/**
	 * 插入能力号码配置关系信息
	 * @param tmpPoolAbility
	 * @return
	 */
	public int doInsertPoolAbility(PoolAbilityInfoBean tmpPoolAbility){
		return poolAbilityInfoDao.insert(coreDao, conversion(tmpPoolAbility));
	}
	/**
	 * 编辑能力号码配置关系信息
	 * @param tmpPoolAbility
	 * @return
	 */
	public int doEditPoolAbility(PoolAbilityInfoBean tmpPoolAbility){
		return poolAbilityInfoDao.update(coreDao, conversion(tmpPoolAbility));
	}
	 /**
     * 根据ID查询相应信息
     * @param pool_id
     * @return
     */
    public PoolNumberBean queryPoolNumberById(String pool_id){
    	PoolNumber poolNumber=poolNumberDao.queryPoolNumberById(coreDao, pool_id);
    	if(null==poolNumber)return null;
    	return conversion(poolNumber);
    }
	 /**
	 * 如果这两个dao方法获取的id对应相等，则在前台设置无法选择，其余则设置可以选择；
	 * 将这个能否选择的判断信息，放入comment_info中，然后在前台进行判断，设置html
	 * @param app_key 应用标识
	 * @param cust_account 账号标识
	 * @param abilityType 能力类型
	 * @return
	 */
    public List<PoolAbilityInfoBean> manageAbiByAccId(String app_key,String cust_account ,String abilityType){
    	List<PoolAbilityInfoBean> resultlist=new ArrayList<PoolAbilityInfoBean>();
    	//后面所需返回的list
		List<PoolAbilityInfo> poolAbiOneList=new ArrayList<PoolAbilityInfo>();
		List<PoolAbilityInfo> poolAbiTwoList=new ArrayList<PoolAbilityInfo>();
		// 根据账号id，查询该账号下所有已配置的号码，它们所配置的能力
		poolAbiOneList = poolAbilityInfoDao.getAllAbiByAccId(coreDao, cust_account, app_key, abilityType);
		                                  //getAllAbiByAccId(dao, cuaccountId, abilityType);
		poolAbiTwoList = poolAbilityInfoDao.getAbiIdByAccIdAbi(coreDao, cust_account, app_key, abilityType);
										  //getAbiIdByAccIdAbi(dao, accountId, abilityType);
		
		// comment_info为空：本账号下配置了其他能力但未配置本能力的号码；若为conf则表示本账号下已配置的能力
		for(PoolAbilityInfo poolAbiOne : poolAbiOneList){
			for(PoolAbilityInfo poolAbiTwo : poolAbiTwoList){
				if(poolAbiTwo.getId() == poolAbiOne.getId()){
					poolAbiOne.setComment_info("conf");
					break;
				}
			}
		}
		
		// 号码归属地为空，则设置为未知
		for(PoolAbilityInfo poolAbiOne : poolAbiOneList){
			if(poolAbiOne.getNumber_vest_name().equals("")){
				poolAbiOne.setNumber_vest_name("未知");
			}
			resultlist.add(conversion(poolAbiOne));
		}
		
		return resultlist;
    }
	/**
	 * 转化
	 * @param menu
	 * @return
	 */
	private PoolAbilityInfoBean conversion(PoolAbilityInfo bean){
		PoolAbilityInfoBean poolBean=new PoolAbilityInfoBean();
		BeanUtils.copyProperties(bean, poolBean);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return poolBean;
	}
	/**
	 * 转化
	 * @param menuBean
	 * @return
	 */
	private PoolAbilityInfo conversion(PoolAbilityInfoBean bean){
		PoolAbilityInfo pool=new PoolAbilityInfo();
		BeanUtils.copyProperties(bean,pool);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return pool;
	}
	
	
	/**
	 * 转化
	 * @param menu
	 * @return
	 */
	private PoolNumberBean conversion(PoolNumber bean){
		PoolNumberBean poolBean=new PoolNumberBean();
		BeanUtils.copyProperties(bean, poolBean);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return poolBean;
	}
	/**
	 * 转化
	 * @param menuBean
	 * @return
	 */
	private PoolNumber conversion(PoolNumberBean bean){
		PoolNumber pool=new PoolNumber();
		BeanUtils.copyProperties(bean,pool);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return pool;
	}
	
}
